package com.ruoyi.common.utils;

import cn.hutool.json.JSONUtil;
import de.odysseus.staxon.json.JsonXMLConfig;
import de.odysseus.staxon.json.JsonXMLConfigBuilder;
import de.odysseus.staxon.json.JsonXMLInputFactory;
import de.odysseus.staxon.json.JsonXMLOutputFactory;
import de.odysseus.staxon.xml.util.PrettyXMLEventWriter;
import lombok.extern.slf4j.Slf4j;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

@Slf4j
public class StaxonUtils {

    /**
     * json string convert to xml string
     */
    public static String json2PrettyXml(String json){
        StringReader input = new StringReader(json);
        StringWriter output = new StringWriter();
        JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false).repairingNamespaces(false).build();
        try {
            XMLEventReader reader = new JsonXMLInputFactory(config).createXMLEventReader(input);
            XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);
            writer = new PrettyXMLEventWriter(writer);
            writer.add(reader);
            reader.close();
            writer.close();
        } catch( Exception e){
            log.error("json2PrettyXml error"+e.getMessage(),e);
        } finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                log.error("json2PrettyXml1 error"+e.getMessage(),e);
            }
        }
        return output.toString();
    }
    /**
     * 功能描述 map 转为xml
     * @author java小金刚
     * @date 2020/8/8
     * @param map
     * @return java.lang.String
    */
    public static String map2xml(Map<String,Object> map){
        String json= JSONUtil.toJsonStr(map);
        return json2xml(json);
    }
    /**
     * 功能描述json字符串转换为xml
     * @author java小金刚
     * @date 2024/05/09
     * @param json
     * @return java.lang.String
    */
    public static String json2xml(String json){
        StringReader input = new StringReader(json);
        StringWriter output = new StringWriter();
        JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false).repairingNamespaces(false).build();
        try {
            XMLEventReader reader = new JsonXMLInputFactory(config).createXMLEventReader(input);
            XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);
            writer = new PrettyXMLEventWriter(writer,"","");
            writer.add(reader);
            reader.close();
            writer.close();
        } catch( Exception e){
            log.error("json2xml error"+e.getMessage(),e);
        } finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                log.error("json2xml1 error"+e.getMessage(),e);
            }
        }
        return output.toString();
    }
/**
 * 功能描述 xml转换为json字符串
 * @author java小金刚
 * @date 2024/05/09
 * @param xml
 * @return java.lang.String
*/
    public static String xml2json(String xml){
        Map<String, Object> requestData = xml2Map(xml);
        return JSONUtil.toJsonStr(requestData);
    }
    /**
     * 功能描述 xml转换为map
     * @author java小金刚
     * @date 2024/05/09
     * @param xml
     * @return java.lang.String
     */
    public static Map<String,Object> xml2Map(String xml){
        StringReader input = new StringReader(xml);
        StringWriter output = new StringWriter();
        JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).autoPrimitive(true).prettyPrint(true).build();
        try {
            XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(input);
            XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output);
            writer.add(reader);
            reader.close();
            writer.close();
        } catch( Exception e){
            log.error("xml2Map error"+e.getMessage(),e);
        } finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                log.error("xml2Map error"+e.getMessage(),e);
            }
        }
        Map<String, Object> requestData = JSONUtil.toBean(output.toString(),Map.class) ;
        convertMapObjectValue2String(requestData);

        return requestData;
    }
    /**
     * 功能描述 将所有value 转换为string
     * @author java小金刚
     * @date 2024/05/09
     * @param requestData
     * @return void
    */
    private static void convertMapObjectValue2String(Map<String, Object> requestData){

        for(Map.Entry<String, Object> entry : requestData.entrySet()){
            Object value=entry.getValue();
            if(value instanceof Map){
                convertMapObjectValue2String((Map)value);
            }else if(value instanceof List){
                List<Map<String, Object>> innerlist=(List<Map<String, Object>>)value;
                for(Map<String, Object> innerItem: innerlist){
                    convertMapObjectValue2String(innerItem);
                }
            }else if(value instanceof String){

            }else{
                if(value!=null){
                    entry.setValue(String.valueOf(value));
                }
            }
        }
    };
}
